﻿using NCP.Core.DataUtil;
using NCP.Core.IoC;
using NCP.Model;
using NCP.Util;
using System.Collections.Generic;

namespace NCP.Service
{
    public class MemberService : BaseService<Member>, IService
    {
        public MemberService()
        {
            _client = DbHelper.GetSqlSugarClient();
        }

        public Member GetByStaffCode(string StaffCode)
        {
            Member member = _client.Queryable<Member>().Where(p => p.Code == StaffCode).First();

            return member;
        }

        public int UpdatePwd(string pwd,int memberId)
        {
            return _client.Updateable<Member>().SetColumns(t => new Member() { Pwd=pwd}).Where(t=>t.Id==memberId).ExecuteCommand();
        }

        public string GetMemberNameByCodeCache(string staffCode)
        {
            string member = _client.Queryable<Member>().Where(p => p.Code == staffCode).Select(p => p.Name).WithCache().First();
            return member;
        }

        public string GetDeptNameByCodeCache(string deptCode)
        {
            string member = _client.Queryable<DeptInfo>().Where(p => p.DeptCode == deptCode).Select(p => p.DeptName).WithCache().First();
            return member;
        }

        public Member Login(string staffcode, string pwd, out bool isSuc)
        {
            if (staffcode.Equals("sysadmin") && pwd == HashEncrypt.Md5Hash("admin@123"))
            {
                Member admin = new Member() { Id = -11, Name = "系统管理员", Code = "sysadmin" };
                isSuc = true;
                return admin;
            }
            Member member = _client.Queryable<Member>().First(t => t.Code == staffcode && t.Status=="1");


            if (member != null&&(member.Pwd==pwd||(string.IsNullOrEmpty(member.Pwd) && pwd== HashEncrypt.Md5Hash("000000"))))
            {
                isSuc = true;
                return member;
            }
            isSuc = false;
            return null;
        }
        public Config GetRegisterValue(string type)
        {
            return _client.Queryable<Config>().Where(t => t.type == type).First();
        }
    }
}
